How to truncate the Cassandra table?

Member

by berta , in category: SQL , a year ago

How to truncate the Cassandra table?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by sister , a year ago

@berta 

To truncate a table in Cassandra, you can use the TRUNCATE command. Here's the syntax:

1
TRUNCATE TABLE tablename;


For example, if you want to truncate a table called users, you would use the following command:

1
TRUNCATE TABLE users;


This will delete all rows in the table and reset the primary key sequence to its initial value. It will also release any disk space used by the table.


Keep in mind that TRUNCATE is a DDL (Data Definition Language) operation, which means that it acquires a global lock on the table and blocks all reads and writes to the table until the operation is complete. This can cause significant performance issues on a heavily loaded cluster, so it is generally recommended to avoid using TRUNCATE on large tables or in high-concurrency environments.


If you only need to delete a small number of rows from a table, it may be more efficient to use the DELETE statement instead of TRUNCATE. The DELETE statement allows you to specify a WHERE clause to specify the rows you want to delete, and it does not block reads or writes to the table while the operation is in progress.